import {
  createRouter,
  createWebHashHistory,
} from "vue-router";

const router = createRouter({
  // 一定要有的属性
  history: createWebHashHistory(), // 路由的模式，常见有两个hash和history(浏览器历史记录)
  // 路由表配置信息
  routes: [
    {
      path: "/", // 路径，就是url中访问的地址
      name: "Index", // 名字，是代码中操作的关键词，用户看不到。name不能重复
      component: () => import("../views/Index.vue"), // 访问这个路劲的时候展示的组件
    },
    {
      path: "/category",
      name: "Category",
      component: () => import("../views/Category.vue"),
    },
    {
      path: "/activity",
      name: "Activity",
      component: () => import("../views/Activity.vue"),
      
    },
    {
      path: "/cart",
      name: "Cart",
      component: () => import("../views/Cart.vue"),
      meta: {
        requiresAuth: true 
      },
    },
    {
      path: "/user",
      name: "User",
      component: () => import("../views/User.vue"),
      meta: {
        requiresAuth: true 
      },
    },
    {
      path: "/login",
      name: "Login",
      component: () => import("../views/UserInfo/Login.vue"),
     
      meta: {
        hideTabBar: true, // 是否需要隐藏底部导航
      },
    },
    {
      path: "/register",
      name: "Register",
      component: () => import("../views/UserInfo/Register.vue"),
     
      meta: {
        hideTabBar:true
        // requiresAuth: true 
      },
    },
    {
      path: "/detail",
      name: "Detail",
      component: () => import("../views/Detail.vue"),
      meta: {
        hideTabBar: true, // 是否需要隐藏底部导航
      },
    },
    ,
    {
      path: "/modinfo",
      name: "ModInfo",
      component: () => import("../views/UserInfo/ModInfo.vue"),
    },
    {
      path: "/modpwd",
      name: "ModPwd",
      component: () => import("../views/UserInfo/ModPwd.vue"),
    },
  ],
});

// 获取token，判断是否登录，此函数可单独封装，在utils文件夹新建auth.js，将此函数填写在此文件并导出，在这里导入
function isLogined() {
  return sessionStorage.getItem("token");
}

// beforeEach 每一次跳转之前执行
//  to 表示到哪里去
//  from 表示从哪里来
router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {
    // 判断有没有登录
    // 此处需要实现登录判断路径
    if (isLogined()) {
      next();
    } else {
      next({
        name: "Login",
      });
    }
  } else {
    next();
  }
});

export default router;